package com.ledim.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ChatRoomBean;
import com.ledim.itf.LedimOnItemClickListener;
import com.ledim.utils.FrescoUtil;
import com.letv.android.client.LetvSDK;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;
import java.util.ArrayList;
import java.util.List;

public class GoneRoomsAdapter extends LedimBaseListAdapter<ChatRoomBean> {

    public boolean mSelectedType;
    public ArrayList<ChatRoomBean> mSelectedRooms;
    public LedimOnItemClickListener<ChatRoomBean> mListener;

    public GoneRoomsAdapter(Context context, LedimOnItemClickListener listener,
                            List<ChatRoomBean> list, boolean mSelectedType) {
        super(context);
        this.mSelectedType = mSelectedType;
        mSelectedRooms = new ArrayList<ChatRoomBean>();
        this.mListener = listener;
        setDatas(list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_gone_all_rooms, null);
            holder.mCover =
                    (SimpleDraweeView) convertView.findViewById(R.id.items_gone_all_rooms_photo);
            holder.mPhoto =
                    (SimpleDraweeView) convertView.findViewById(R.id.items_gone_all_rooms_owner);
            holder.mName = (TextView) convertView.findViewById(R.id.items_gone_all_rooms_name);
            holder.mStatus = (TextView) convertView.findViewById(R.id.items_gone_all_rooms_status);
            holder.mSelectLayout =
                    convertView.findViewById(R.id.items_gone_all_rooms_select_layout);
            holder.mBottom = convertView.findViewById(R.id.items_gone_all_rooms_bottom);
            holder.mSelected = (ImageView) convertView.findViewById(R.id.selected_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ChatRoomBean room = getItem(position);
        if (position == getCount() - 1) {
            holder.mBottom.setVisibility(View.VISIBLE);
        } else {
            holder.mBottom.setVisibility(View.GONE);
        }
        if (room.cover != null && !TextUtils.isEmpty(room.cover.thumb)) {
            FrescoUtil.loadUrl(room.cover.thumb, holder.mCover);
        }
        if (room.user != null && room.user.avatar != null &&
                !TextUtils.isEmpty(room.user.avatar.thumb)) {
            FrescoUtil.loadUrl(room.user.avatar.thumb, holder.mPhoto);
        }
        if (room.name != null && !room.name.equals("null") && room.name.length() > 0) {
            holder.mName.setText(room.name);
        }
        if (room.status == 0) {
            holder.mStatus.setText("播放结束");
            holder.mStatus.setBackgroundResource(R.drawable.rect_round_room_status_end);
        } else if (room.status == 1) {
            holder.mStatus.setText("正在播放");
            holder.mStatus.setBackgroundResource(R.drawable.rect_round_room_status_play);
        }

        if (mSelectedType) {
            holder.mSelectLayout.setVisibility(View.VISIBLE);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickCheckEpisodes(holder.mSelected, room);
                    if (mListener != null) {
                        mListener.onItemClick(position, room, v);
                    }
                }
            });
            if (mSelectedRooms.contains(room)) {
                holder.mSelected.setImageResource(R.drawable.check);
            } else {
                holder.mSelected.setImageResource(R.drawable.unchecked);
            }
        } else {
            holder.mSelectLayout.setVisibility(View.GONE);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IVideo roomVideo = new IVideo();
                    roomVideo.setmVid(0);
                    LetvSDK.getInstance().playRoom(context, roomVideo, room);
                }
            });
        }
        return convertView;
    }

    private void onClickCheckEpisodes(ImageView mCheckMArk, ChatRoomBean roomBean) {

        if (mSelectedRooms.contains(roomBean)) {
            mCheckMArk.setImageResource(R.drawable.unchecked);
            mSelectedRooms.remove(roomBean);
        } else {
            mSelectedRooms.add(roomBean);
            mCheckMArk.setImageResource(R.drawable.check);
        }
    }

    public ArrayList<ChatRoomBean> getSelectedCards() {
        return mSelectedRooms;
    }

    public boolean isSelectedAll() {
        return mSelectedRooms.size() == getCount();
    }

    public int getSelectedCount() {
        return mSelectedRooms.size();
    }

    public void clearCards() {
        getDatas().removeAll(mSelectedRooms);
        mSelectedRooms.clear();
        notifyDataSetChanged();

    }

    public void selectAll() {
        mSelectedRooms.clear();
        mSelectedRooms.addAll(getDatas());
        notifyDataSetChanged();
    }

    class ViewHolder {

        private SimpleDraweeView mCover;
        private SimpleDraweeView mPhoto;
        private TextView mName;
        private TextView mStatus;
        private View mSelectLayout;
        private View mBottom;
        private ImageView mSelected;
    }
}
